package controller;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import service.DeptService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/dept")
public class DeptIndexServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(req.getServletContext());
        DeptService deptService = context.getBean(DeptService.class);

        String name = deptService.getNameById(2);

        req.setAttribute("deptname", name);

        req.getRequestDispatcher("/WEB-INF/dept.jsp").forward(req, resp);


    }




   /* @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        //Web下的容器基本会多一个能力，就是获取ServletContext对象
        //context.getServletContext();

        //设置配置元数据，这里设置的是配置类的全称
        context.setConfigLocation("config.AppConfig");
        //相当于更新容器，也就是启动容器，因为你刚刚配置过
        context.refresh();

        DeptService deptService = context.getBean(DeptService.class);

        String name = deptService.getNameById(2);

        req.setAttribute("deptname", name);

        req.getRequestDispatcher("/WEB-INF/dept.jsp").forward(req,resp);


    }*/
}
